$(function(){
	
	
	$.sValid.init({//注册验证
        rules:{
        	phone:{
            	required:true,
            	digits:true,
            	maxlength:11,
            	minlength:11
            },
        	authcode:"required",
            userpwd:"required"
        },
        messages:{
            phone:{
            	required:"手机号必须填写！",
            	digits:"请输入正确格式的手机号！",
            	maxlength:"请输入正确格式的手机号！",
            	minlength:"请输入正确格式的手机号！"          	
            }, 
            authcode:"验证码必填!",
            userpwd:"密码必填!"
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                $(".error-tips").html(errorHtml).show();
            }else{
                $(".error-tips").html("").hide();
            }
        }  
    });
	//验证码倒计时函数
	var btnauthcode = $('#btnauthcode');
	var countdown=60; 
	function settime() { 
		if (countdown == 0) {
			btnauthcode.removeClass("l-btn-login-disable");    
			btnauthcode.text("获取验证码"); 
			countdown = 60; 
		} else {
			btnauthcode.addClass("l-btn-login-disable"); 
			btnauthcode.text("重新发送(" + countdown + ")"); 
			countdown--;
			setTimeout(function() { 
				settime() 
			},1000);
		}
	};
    //获取验证码
    $('#btnauthcode').click(function(){
    	if (countdown!=60) {
    		return false;
    	};
    	var phone = $('#phone').val();
    	console.log(phone)
    	if ((/^\d+$/.test(phone))&&(phone.length==11)) {
    		$(".error-tips").html("").hide();
    		$.ajax({
				type:'post',
				url:ApiUrl+"/user/messages/authcode",	
				data:{phone:phone,isVerifyPhone:false},
				dataType:'json',
				success:function(result){
					console.log(result);
					if(result.rspCode==0){
						settime();
					}else{
						$.sDialog({
                            skin:"red",
                            content:result.rspDesc,
                            okBtn:false,
                            cancelBtn:false
                        });
					}
				}
			});	
    	}else{
    		 $(".error-tips").html("<p>请输入正确的手机号！</p>").show();
    	};
    });

	$('input[name="gender"]').click(function(){
		$('#gender').val($(this).val());
	});
	$('#loginbtn').click(function(){
		var phone = $("input[name=phone]").val();
		var password = $("input[name=pwd]").val();
		var authcode = $("input[name=authcode]").val();
		if($.sValid()){
			$.ajax({//判断验证码
				type:'post',
				url:ApiUrl+"/user/messages/resetPassword",
				data:{phone:phone,n_rand:authcode,password:password},
				dataType:'json',
				success:function(result){
					if(result.rspCode==0){
						// location.href = WapSiteUrl+'/tmpl/member/login.html';
						window.location.replace(WapSiteUrl+'/tmpl/member/login.html')
						// history.go(-2);
					}else{
						$.sDialog({
							skin:"red",
							content:'失败，请重试。',
							okBtn:false,
							cancelBtn:false
						});
					}
				}
			});
		}
	});
});